﻿using Report_Dal.Web;
using Report_IDal.Web;
using Report_IService.Web;
using Report_Model.Db;
using Report_Service.Basic;
using Report_Unity.Basic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Report_Service.Web
{
    public class T_CompanyService : BaseService<T_Company>, IT_CompanyService
    {
        private IT_CompanyDal T_CompanyDal = new T_CompanyDal(ConfigSource.tab_web);
        public override void SetCurrentDal()
        {
            base.BaseDal = T_CompanyDal;
        }

        public ResultInfo AddCompany(string hid,string name,string desc,string userid)
        {
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{hid}必传");
            if (string.IsNullOrEmpty(name))
                return CodeStatus.error_201.GetStatus($"{name}必传");
            if (string.IsNullOrEmpty(userid))
                return CodeStatus.error_201.GetStatus($"{userid}必传");

            T_Company company = GetT_Company(hid,name,desc,userid);

            if (!Add(company))
                return CodeStatus.error_301.GetStatus("添加失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo EditCompany(string cid, string hid, string name, string desc, string userid)
        {
            if (string.IsNullOrEmpty(cid))
                return CodeStatus.error_201.GetStatus($"{cid}必传");
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{hid}必传");
            if (string.IsNullOrEmpty(name))
                return CodeStatus.error_201.GetStatus($"{name}必传");
            if (string.IsNullOrEmpty(userid))
                return CodeStatus.error_201.GetStatus($"{userid}必传");

            T_Company company = Get(a => a.company_id == cid && a.hospital_id == hid).FirstOrDefault();
            if (company == null)
                return CodeStatus.error_202.GetStatus("无单位信息");

            company.company_name = name;
            company.company_desc = desc;
            company.up_userid = userid;
            company.up_time = DateTime.Now;

            if (!Up(company))
                return CodeStatus.error_301.GetStatus("修改失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo DelCompany(string cid, string hid)
        {
            if (string.IsNullOrEmpty(cid))
                return CodeStatus.error_201.GetStatus($"{cid}必传");
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{hid}必传");

            T_Company company = Get(a => a.company_id == cid && a.hospital_id == hid).FirstOrDefault();
            if (company == null)
                return CodeStatus.error_202.GetStatus("无单位信息");

            if (!Del(company))
                return CodeStatus.error_301.GetStatus("删除失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo GetListCompany(string hid)
        {
            var list = GetAsNoTracking(a => a.hospital_id == hid).Select(a => new { hid = a.hospital_id,cid = a.company_id, name = a.company_name, desc = a.company_desc }).ToList();
            ResultInfo res = CodeStatus.success.GetStatus();
            res.data = new { list = list};
            return res;
        }

        private T_Company GetT_Company(string hid, string name, string desc, string userid)
        {
            T_Company company = new T_Company();
            company.company_id = GetNextId(a=>a.company_id);
            company.hospital_id = hid;
            company.company_name = name;
            company.company_desc = desc;
            company.create_userid = userid;
            company.create_time = DateTime.Now;

            return company;
        }
    }
}
